package com.ht.medicaltreatment.viewmodel

import android.os.Environment
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnDownloadListener
import com.hjq.http.listener.OnHttpListener
import com.hjq.http.listener.OnUpdateListener
import com.hjq.http.model.HttpMethod
import com.ht.medicaltreatment.api.RetrofitUtils
import com.ht.medicaltreatment.base.BaseUrl
import com.ht.medicaltreatment.bean.Page
import com.ht.medicaltreatment.bean.PostFileBean
import com.ht.medicaltreatment.bean.ProjectBean
import com.ht.medicaltreatment.bean.SampleDetailBean
import com.ht.medicaltreatment.bean.SampleListData2
import com.ht.medicaltreatment.bean.SuccessBean
import com.ht.medicaltreatment.http.api.BuildSampleAPI
import com.ht.medicaltreatment.http.api.DeleteSampleAPI
import com.ht.medicaltreatment.http.api.PostFileAPI
import com.ht.medicaltreatment.http.api.SampleDetailAPI
import com.ht.medicaltreatment.http.api.UpdateSampleAPI
import com.xgst.driverlesscars.api.ApiService
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import java.io.File

class SampleViewModl :BaseViewModel(){

    fun sampleList(page:Int,name:String = "",uid:String = "",barCode:String = "",onResult:(SampleListData2)->Unit){
        /*EasyHttp.post(this)
            .api(SampleListAPI(page,barCode, sampleName = name, uid = uid))
            .request(object : OnHttpListener<SampleListBean>{
                override fun onHttpSuccess(result: SampleListBean) {
                    onResult(result.data)
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message!!)
                }
            })*/

        MainScope().launch {
            val bean = RetrofitUtils.initRetrofit(ApiService::class.java)
                .sampleList(name,uid,barCode,"$page","20")
            when(bean){
                is ApiResult.Success -> onResult(bean.data)
                is ApiResult.Error ->  dismissProgress(bean.errorMessage)
            }
        }
    }

    fun create(bean:BuildSampleAPI,onResult:()->Unit){
        EasyHttp.post(this)
            .api(bean)
            .request(object : OnHttpListener<SuccessBean>{
                override fun onHttpSuccess(result: SuccessBean) {
                    if(result.code == 200){
                        onResult()
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message!!)
                }
            })
    }

    fun updateSample(bean: UpdateSampleAPI, onResult:()->Unit){
        EasyHttp.post(this)
            .api(bean)
            .request(object : OnHttpListener<SuccessBean>{
                override fun onHttpSuccess(result: SuccessBean) {
                    if(result.code == 200){
                        onResult()
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception) {
                    ToastUtils.showShort(e.message)
                }
            })
    }

    fun delete(id : Int ,onResult:()->Unit){
        val list = arrayListOf(id)
        EasyHttp.post(this)
            .api(DeleteSampleAPI(list))
            .request(object : OnHttpListener<SuccessBean>{
                override fun onHttpSuccess(result: SuccessBean) {
                    if(result.code == 200){
                        onResult()
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message!!)
                }
            })
    }

    fun download(onProgress:(Int)->Unit){
        EasyHttp.download(this)
            .method(HttpMethod.GET)
            .file(File(Environment.getExternalStorageDirectory(), "惠通医疗/sampleInfo_template.xlsx"))
            .url("${BaseUrl.baseUrl()}/upload/gcpExcelExample/sampleInfo_template.xlsx")
            .listener(object : OnDownloadListener {
                override fun onDownloadProgressChange(file: File?, progress: Int) {
                    onProgress(progress)
                }

                override fun onDownloadSuccess(file: File?) {

                }

                override fun onDownloadFail(file: File?, e: java.lang.Exception?) {

                }
            })
            .start()
    }

    fun postFile(path:String,onResult: (ArrayList<Page>) -> Unit){
        val file = File(path)
        LogUtils.i(file.absolutePath)
        EasyHttp.post(this)
            .api(PostFileAPI(multipartFile = file, projectId = 1))
            .request(object : OnUpdateListener<PostFileBean> {
                override fun onUpdateProgressChange(progress: Int) {

                }

                override fun onUpdateFail(e: java.lang.Exception?) {

                }

                override fun onUpdateSuccess(result: PostFileBean?) {

                }
            })
    }

    fun detail(id:Int,onResult:(SampleDetailBean.DataBean)->Unit,onError:(String)->Unit){
        EasyHttp.post(this)
            .api(SampleDetailAPI("$id"))
            .request(object : OnHttpListener<SampleDetailBean>{
                override fun onHttpSuccess(result: SampleDetailBean) {
                    if(result.code == 200){
                        onResult(result.data)
                    }else{
                        onError(result.message)
                    }
                }

                override fun onHttpFail(e: java.lang.Exception) {
                    onError(e.message?:"")
                }
            })
    }

    fun projects(onResult:(MutableList<ProjectBean>)->Unit){
        MainScope().launch {
            val bean = RetrofitUtils.initRetrofit(ApiService::class.java).projects()
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error ->  ToastUtils.showShort(bean.errorMessage)
            }
        }
    }
}
